import RequestService from '../httpRequest'
import {getServiceUrl} from '../api'

export default {
    // 已绑设备
     getAgentBindDevices(agentId, callback) {
         RequestService.sendRequest()
             .url(`${getServiceUrl()}/api/v1/device/bind/${agentId}`)
             .method('GET')
             .success((res) => {
                 RequestService.clearRequestTime();
                 callback(res);
             })
             .fail((err) => {
                 console.error('获取设备列表失败:', err);
                 RequestService.reAjaxFun(() => {
                     this.getAgentBindDevices(agentId, callback);
                 });
             }).send();
     },
    // 解绑设备
    unbindDevice(device_id, callback) {
          RequestService.sendRequest()
            .url(`${getServiceUrl()}/api/v1/device/unbind`)
            .method('POST')
            .data({ deviceId: device_id })
            .success((res) => {
              RequestService.clearRequestTime();
              callback(res);
            })
            .fail((err) => {
              console.error('解绑设备失败:', err);
              RequestService.reAjaxFun(() => {
                this.unbindDevice(device_id, callback);
              });
            }).send();
    },
    // 绑定设备
    bindDevice(agentId, deviceCode, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/api/v1/device/bind/${agentId}/${deviceCode}`)
            .method('POST')
            .success((res) => {
                RequestService.clearRequestTime();
                callback(res);
            })
            .fail((err) => {
                console.error('绑定设备失败:', err);
                RequestService.reAjaxFun(() => {
                    this.bindDevice(agentId, deviceCode, callback);
                });
            }).send();
    },
}